package com.graduationdesign.zengyan.graduationdesignapp.ui.activities.event;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.graduationdesign.zengyan.graduationdesignapp.BaseApplication;
import com.graduationdesign.zengyan.graduationdesignapp.R;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.AccountManager;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.base.Result;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.Event;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.EventResultModel;
import com.graduationdesign.zengyan.graduationdesignapp.dataLayer.model.resultModel.SignUpResult;
import com.graduationdesign.zengyan.graduationdesignapp.ui.activities.PresenterInjection;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.BaseActivity;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.IntentKey;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.BaseErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.base.errorhandle.CommonErrorView;
import com.graduationdesign.zengyan.graduationdesignapp.ui.contact.ProfileContact;
import com.graduationdesign.zengyan.graduationdesignapp.ui.presenter.ProfilePresenter;
import com.graduationdesign.zengyan.graduationdesignapp.utils.ToastUtils;
import com.graduationdesign.zengyan.graduationdesignapp.utils.Tools;
import com.graduationdesign.zengyan.graduationdesignapp.utils.ViewAnimation;

import butterknife.BindView;

public class EventDetailActivity extends BaseActivity implements ProfileContact.ShowSignUpResult {

    @BindView(R.id.progress_bar)
    ProgressBar progressBar;

    @BindView(R.id.webView)
    WebView webView;

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.avatar)
    SimpleDraweeView avatar;

    @BindView(R.id.signup_btn)
    Button signUpBtn;

    private Event mEvent;
    private boolean isSignUp;
    private ProfileContact.Presenter presenter;

    @Override
    protected int provideLayoutId() {
        return R.layout.activity_event_detail;
    }

    @Override
    protected Boolean isFullScreen() {
        return false;
    }

    @Override
    protected void initArgument() {
        EventResultModel eventResultModel = getIntent().getParcelableExtra(IntentKey.Event);
        isSignUp = eventResultModel.isSignUp();
        mEvent = eventResultModel.getEvent();
    }

    @Override
    protected void initPresenter() {
        BaseErrorView mErrorView = new CommonErrorView(this);
        presenter = PresenterInjection.provideProfilePresenter(
                ProfilePresenter.newBuilder()
                        .setBaseErrorView(mErrorView)
                        .setShowSignUpResult(this)

        );
    }

    @Override
    protected void initToolbar() {
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle(null);
        Tools.setSystemBarColor(this, R.color.grey_20);
    }

    @RequiresApi(api = Build.VERSION_CODES.M)
    @Override
    protected void initView() {
        title.setText("「" + mEvent.getTitle() + "」");
        if (isSignUp) {
            signUpBtn.setText("已报名");
            signUpBtn.setTextColor(BaseApplication.Instance.getColor(R.color.overlay_light_40));
        }else {
            signUpBtn.setText("报名");
            signUpBtn.setOnClickListener(v -> {
                presenter.signUp(AccountManager.getInstance().getCurrentStuNum(),mEvent.getId());
            });
        }
        loadWebFromUrl();
    }

    private void loadWebFromUrl() {
        progressBar.setProgress(0);
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setAlpha(1.0f);
        webView.getSettings().setJavaScriptEnabled(true);//启用js
        webView.getSettings().setBlockNetworkImage(false);//解决图片不显示
        webView.setWebChromeClient(new WebChromeClient() {


            public void onProgressChanged(WebView view, int progress) {
                progressBar.setProgress(progress);
                if (progress == 100) {
                    ViewAnimation.fadeOut(progressBar);
                }
            }
        });
        webView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
        });
        webView.loadUrl(mEvent.getDetailUrl());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_refresh_setting, menu);
        Tools.changeMenuIconColor(menu, getResources().getColor(R.color.grey_60));
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
        } else if (item.getItemId() == R.id.action_refresh) {
            loadWebFromUrl();
        } else {
            Toast.makeText(getApplicationContext(), item.getTitle(), Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void start() {

    }

    @Override
    public void onPause() {
        super.onPause();
        webView.onPause();
    }

    @Override
    public void onResume() {
        webView.onResume();
        super.onResume();
    }

    @Override
    public void onDestroy() {
        if (webView != null) {
            webView.destroy();
            webView = null;
        }
        super.onDestroy();
    }

    @TargetApi(Build.VERSION_CODES.M)
    @Override
    public void showSignUpResult(Result<SignUpResult> result) {
        ToastUtils.showToast(this,"报名成功");
        signUpBtn.setText("已报名");
        signUpBtn.setTextColor(BaseApplication.Instance.getColor(R.color.overlay_light_40));
        signUpBtn.setOnClickListener(v -> {});
    }
}
